package com.wolfalpha.utron.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;

import com.wolfalpha.utron.activity.LoginActivity;
import com.wolfalpha.utron.activity.MainActivity;
import com.wolfalpha.utron.app.AppContext;
import com.wolfalpha.utron.app.AppManager;

/**
 * UI帮助类
 *
 * @author Administrator
 */
public class UIHelper {

    public final static String WEB_STYLE = "<style>* {font-size:16px;line-height:20px;} p {color:#333;} a {color:#3E62A6;} img {max-width:310px;} "
            + "img.alignleft {float:left;max-width:120px;margin:0 10px 5px 0;border:1px solid #ccc;background:#fff;padding:2px;} "
            + "pre {font-size:9pt;line-height:12pt;font-family:Courier New,Arial;border:1px solid #ddd;border-left:5px solid #6CE26C;background:#f6f6f6;padding:5px;} "
            + "a.tag {font-size:15px;text-decoration:none;background-color:#bbd6f3;border-bottom:2px solid #3E6D8E;border-right:2px solid #7F9FB6;color:#284a7b;margin:2px 2px 2px 0;padding:2px 4px;white-space:nowrap;}</style>";


    /**
     * 提示未登录
     *
     * @param activity
     */
    public static void showNotLogined(final Activity activity) {

        System.out.println("aaaa");

        final com.wolfalpha.utron.view.AlertDialog ad = new com.wolfalpha.utron.view.AlertDialog(activity);
        ad.setTitle("请先登录");
        ad.setMessage("使用当前功能需要登录,现在去登录？");
        ad.setPositiveButton("现在", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UIHelper.showActivityAndFinish(activity, LoginActivity.class);
                ad.dismiss();
            }
        });
        ad.setNegativeButton("稍后", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ad.dismiss();
            }
        });

    }


    /**
     * 显示主界面MainActivity，并关闭当前
     *
     * @param activity
     */
    public static void showMainAndFinish(Activity activity) {
        Intent intent = new Intent(activity, MainActivity.class);
        activity.startActivity(intent);
        activity.finish();
    }

    /**
     * 调用系统安装了的应用分享
     *
     * @param context
     * @param title
     * @param url
     */
    public static void showShareMore(Activity context, final String title,
                                     final String url) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_SUBJECT, "分享：" + title);
        intent.putExtra(Intent.EXTRA_TEXT, title + " " + url);
        context.startActivity(Intent.createChooser(intent, "选择分享"));
    }

    /**
     * 打开浏览器
     *
     * @param context
     * @param url
     */
    public static void openBrowser(Context context, String url) {
        try {
            Uri uri = Uri.parse(url);
            Intent it = new Intent(Intent.ACTION_VIEW, uri);
            context.startActivity(it);
        } catch (Exception e) {
            e.printStackTrace();
            ToastMessage(context, "打开失败", 500);
        }
    }

    /**
     * 提示消息
     *
     * @param msg
     */
    public static void ToastMessage(Context cont, String msg) {
        Toast toast = Toast.makeText(cont, msg, Toast.LENGTH_SHORT);
        //设置Toast位置
        toast.setGravity(Gravity.BOTTOM, 0, 0);
        toast.show();
    }

    public static void ToastMessage(Context cont, int msg) {
        Toast toast = Toast.makeText(cont, msg, Toast.LENGTH_SHORT);
        //设置Toast位置
        toast.setGravity(Gravity.BOTTOM, 0, 0);
        toast.show();
    }

    public static void ToastMessage(Context cont, String msg, int time) {
        Toast toast = Toast.makeText(cont, msg, time);
        //设置Toast位置
        toast.setGravity(Gravity.BOTTOM, 0, 0);
        toast.show();
    }

    /**
     * 关闭当前Activity的监听
     *
     * @param activity
     * @return
     */
    public static View.OnClickListener finish(final Activity activity) {
        return new View.OnClickListener() {
            public void onClick(View v) {
                activity.finish();
            }
        };
    }

    /**
     * 打开主界面但不关闭当前界面
     *
     * @param context
     */
    public static void showMain(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        context.startActivity(intent);
    }

    /**
     * 打开指定Activity并关闭当前界面
     *
     * @param context
     * @param cls
     */
    @SuppressWarnings("rawtypes")
    public static void showActivityAndFinish(Context context, Class cls) {
        Intent intent = new Intent(context, cls);
        context.startActivity(intent);
        ((Activity) context).finish();
    }

    /**
     * 打开指定Activity,不关闭当前界面
     *
     * @param context
     * @param cls
     */
    @SuppressWarnings("rawtypes")
    public static void showActivity(Context context, Class cls) {
        // TODO Auto-generated method stub
        Intent intent = new Intent(context, cls);
        context.startActivity(intent);
    }

    /**
     * 打开指定Activity,不关闭当前界面
     *
     * @param context
     * @param cls
     */
    @SuppressWarnings("rawtypes")
    public static void showActivity(Context context, Class cls, Bundle bundle) {
        // TODO Auto-generated method stub
        Intent intent = new Intent(context, cls);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }


    /**
     * 打开index context
     *
     * @param activity
     */
    public static void showIndex(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        context.startActivity(intent);
    }

    /**
     * 清除应用的缓存
     *
     * @param activity
     */
    public static void clearAppCache(Activity activity) {
        final AppContext ac = (AppContext) activity.getApplication();
        final Handler handler = new Handler() {
            public void handleMessage(Message msg) {
                if (msg.what == 1) {
//                    ToastMessage(ac, "清除成功");
                } else {
//                    ToastMessage(ac, "清除失败");
                }
            }
        };
        new Thread() {
            public void run() {
                Message msg = new Message();
                try {
                    AppContext.getInstance().clearAppCache();
                    msg.what = 1;
                } catch (Exception e) {
                    e.printStackTrace();
                    msg.what = -1;
                }
                handler.sendMessage(msg);
            }
        }.start();
    }

    /**
     * 退出
     *
     * @param cont
     */
    public static void Exit2(final Context cont) {
        AppManager.getAppManager().AppExit(cont);
    }

    /**
     * 退出
     *
     * @param cont
     */
    public static void Exit(final Context cont) {
        AlertDialog.Builder builder = new AlertDialog.Builder(cont);
        builder.setIcon(android.R.drawable.ic_dialog_info);
        builder.setTitle("退出应用?");
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                AppManager.getAppManager().AppExit(cont);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.show();
    }

}
